package cn.com.dycc.party_org.team.service;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.common.log.Constants;
import cn.com.dycc.common.log.SystemServiceLog;
import cn.com.dycc.party_org.organize.dao.OrganizeDao;
import cn.com.dycc.party_org.organize.service.NotFoundOrganizeException;
import cn.com.dycc.party_org.team.dao.TeamDao;
import cn.com.dycc.party_org.team.entity.TeamResultEntity;
import cn.com.dycc.party_org.team.param.AddTeamParam;
import cn.com.dycc.party_org.team.param.FindPartyGroupParam;
import cn.com.dycc.util.IntegerUtil;
import cn.com.dycc.util.Paging;

@Service("teamService")
@Transactional(rollbackFor=Exception.class)
public class TeamServiceImpl implements TeamService{
	
	@Resource(name="teamDao")
	private TeamDao teamDao;
	
	@Resource(name="organizeDao")
	private OrganizeDao organizeDao;
	
	@Resource(name="paging")
	private Paging paging;
	
	//增加党组班子 HttpServletRequest request,
	@SystemServiceLog(description=Constants.TEAM_ADD)
	public String addDeptPartyGroup(HttpServletRequest request,AddTeamParam addTeamParam) throws NotFoundTeamException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		//党组id
		if(addTeamParam.getDept_id()==null||addTeamParam.getDept_id().trim().isEmpty()){
			throw new NotFoundTeamException("请选择党组组织!");
		}
		if(!(IntegerUtil.isNumeric(addTeamParam.getDept_id()))){
			throw new NotFoundTeamException("党组组织id只能为整型！");
		}
		
		int count = organizeDao.findCountDeptPartyById(addTeamParam.getDept_id(),org_id);
		if(count==0){
			throw new NotFoundTeamException("选择的党组组织不存在！");
		}
		
		//党组成员
		if(addTeamParam.getUser_id()==null||addTeamParam.getUser_id().trim().isEmpty()){
			throw new NotFoundTeamException("请选择需要增加在党员!");
		}
		if(!(IntegerUtil.isNumeric(addTeamParam.getUser_id()))){
			throw new NotFoundTeamException("增加的党员id只能是整型!");
		}
		
		int countTeam = teamDao.countTeam(addTeamParam.getDept_id(),addTeamParam.getUser_id(),org_id);
		if(countTeam>=1){
			throw new NotFoundTeamException("该党员已存在该党组织班子内!");
		}
		
		//判断党组成员是否存在
		
		
		//order_no只能为整型
		if(addTeamParam.getOrder_no()!=null&&!(IntegerUtil.isNumeric(addTeamParam.getOrder_no().toString()))){
			throw new NotFoundOrganizeException("党组班子成员排序只能为整型！");
		}
		
//		String org_id = "1";
		
		String opt_user_id = request.getSession().getAttribute("id").toString();
//		String opt_user_id = "1";
		
		if(opt_user_id==null){
			throw new NotFoundOrganizeException("请登录！");
		}
		
		addTeamParam.setOpt_user_id(opt_user_id);
		//全部党组标识默认为1
//		addTeamParam.setGroup_flag("1");
		if(addTeamParam.getGroup_flag()==null||addTeamParam.getGroup_flag().trim().isEmpty()){
			throw new NotFoundOrganizeException("请选择党班子标识！");
		}
		addTeamParam.setOrg_id(org_id);
		
		int add = teamDao.addDeptPartyGroup(addTeamParam);
		
		if(add==0){
			throw new NotFoundOrganizeException("增加失败！");
		}
		
		return "增加成功！";
	}
	
	//查询党组班子信息列表
	public List<TeamResultEntity> findPartyGroupList(HttpServletRequest request, FindPartyGroupParam findPartyGroupParam)
			throws NotFoundTeamException {
		
//		if(findPartyGroupParam.getGroup_flag()==null||findPartyGroupParam.getGroup_flag().trim().isEmpty()){
//			throw new NotFoundOrganizeException("请选择查询班子！");
//		}
		
		Integer limit = findPartyGroupParam.getLimit();
		Integer page = findPartyGroupParam.getPage();
		
		if(!(IntegerUtil.isNumeric(page.toString()))){
			throw new NotFoundOrganizeException("查询页码为整型！");
		}
		
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		findPartyGroupParam.setLimit(limit);
		findPartyGroupParam.setOrg_id(org_id);
		findPartyGroupParam.setStart(start);
		
		return teamDao.findPartyGroupList(findPartyGroupParam);
	}
	
	//统计党组织班子人员列表
	public String findPartyGroupListCount(HttpServletRequest request, FindPartyGroupParam findPartyGroupParam)
			throws NotFoundTeamException {

		String org_id = request.getSession().getAttribute("org_id").toString();
		findPartyGroupParam.setOrg_id(org_id);
		return teamDao.findPartyGroupListCount(findPartyGroupParam);
	}
	
	// 删除党员班子人员
	@SystemServiceLog(description=Constants.TEAM_DELETE)
	public String delPartyGroup(HttpServletRequest request,String dept_id, String user_id) throws NotFoundTeamException {
		
		if(dept_id==null||dept_id.trim().isEmpty()){
			throw new NotFoundOrganizeException("请选择需要删除党组班子的所在的党组织！");
		}
		if(!(IntegerUtil.isNumeric(dept_id))){
			throw new NotFoundOrganizeException("需要删除党组班子的所在的党组织为整型！");
		}
		
		if(user_id==null||user_id.trim().isEmpty()){
			throw new NotFoundOrganizeException("请选择需要删除党组班子人员！");
		}
		if(!(IntegerUtil.isNumeric(user_id))){
			throw new NotFoundOrganizeException("删除党组班子人员为整型！");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		int count = teamDao.countTeam(dept_id, user_id,org_id);
		if(count==0){
			throw new NotFoundOrganizeException("需要删除的党组班子人员不存在！");
		}
		
		int i = teamDao.delPartyGroup(dept_id, user_id,org_id);
		if(i==0){
			throw new NotFoundOrganizeException("删除失败！");
		}
		
		return "删除成功！";
	}

}
